package com.hispeed.management.controller;

import com.hispeed.management.configuration.SysConfiguration;
import com.hispeed.management.domain.SysMenuInfoVo;
import com.hispeed.management.domain.SysUserInfoVo;
import com.hispeed.management.service.ISysMenuService;
import com.hispeed.management.service.ISysUserService;
import com.hispeed.management.util.JsonUtil;
import com.hispeed.management.util.MD5;
import com.hispeed.management.util.PublicUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * 首页控制器
 *
 * @author zhangzb
 * @create 2017-06-19 14:57
 **/
@Controller
public class IndexController {

    private Logger logger = LoggerFactory.getLogger(IndexController.class);

    @Autowired
    private ISysMenuService sysMenuService;

    @Autowired
    private ISysUserService sysUserService;

    @Autowired
    private SysConfiguration sysConfiguration;


    @RequestMapping(value="/index")
    public String login(HttpServletRequest request) {

        SysUserInfoVo userInfo = null;
        String errorInfo = "";
        HttpSession httpSession = request.getSession();
        String sessionId = httpSession.getId();

        /* 登录页面的请求，需要从数据库中验证用户信息
         * 若session中有用户信息，则从session中获取 */
        try {

            if(null==httpSession.getAttribute("userInfo")
                    || request.getRequestURL().indexOf("/login")>0) {
                String username = request.getParameter("username");
                String password = request.getParameter("password");

                if(PublicUtil.isEmpty(username)) { //用户名为空直接返回登录页
                    errorInfo = sysConfiguration.loginNamePwdError;
                    request.setAttribute("errorInfo", errorInfo);
                    return "login";
                }
                //从数据库验证用户信息
                userInfo = sysUserService.getUserInfoByLoginName(username);
                //验证用户状态,状态非法进行提示
                if(0 != userInfo.getUserStatus()) {
                    errorInfo = sysConfiguration.userIllegal;
                    request.setAttribute("errorInfo", errorInfo);
                    return "login";
                }
                //验证用户登录密码,密码错误进行提示
                if(!MD5.checkPwd(password, userInfo.getLoginPwd())) {
                    errorInfo = sysConfiguration.loginNamePwdError;
                    request.setAttribute("errorInfo", errorInfo);
                    return "login";
                }
                logger.info("{ sessionId=" + sessionId + ",loginName=" + userInfo.getLoginName()
                    + ",realName=" + userInfo.getRealName() + " }登录系统");

            }

            System.out.println("=========查询菜单========");

            //获取所有菜单
            List<SysMenuInfoVo> menuInfoVoList = sysMenuService.getAllSysMenusInfo();

            System.out.println("=========菜单数量========" + menuInfoVoList.size());

            String menuJson = JsonUtil.toJson(menuInfoVoList);
            request.setAttribute("menuJson", menuJson);

        } catch (Exception e) {

        }
        return "index";
    }

}
